﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SerialIO
{
    /// <summary>
    /// Interaction logic for Logger.xaml
    /// </summary>
    public partial class LogConsole : UserControl, INotifyPropertyChanged
    {
        public LogConsole()
        {
            InitializeComponent();
            _console = this;
        }

        private static LogConsole _console;

        private static StringBuilder _logText = new StringBuilder();

        public string LogText
        {
            get { 
                return _logText.ToString(); 
            }
            set
            {
                _logText = new StringBuilder(value);
                NotifyPropertyChanged();
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public static void WriteLine(string text, params object[] args)
        {
            _logText.AppendFormat(text + Environment.NewLine, args);
            if (_console != null)
            {
                _console.NotifyPropertyChanged("LogText");
            }
        }
    }
}
